package org.uni_hi.lsep.gui;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.uni_hi.lsep.AppManager;
import org.uni_hi.lsep.gui.MainFrame.ContentPanelType;
import org.uni_hi.lsep.text.ChapterNode;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class BrowserFunction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static HtmlPanel panel=null;
	private static UserAgentContext uContext=null;
	private static SimpleHtmlRendererContext rContext=null;
	private static org.lobobrowser.html.parser.DocumentBuilderImpl docBuilder=null;
	private static org.w3c.dom.Document doc = null;

	public BrowserFunction(){
		synchronized (this) {
			Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING);
			init();
		}
	}
	
	public void setHTMLPanel(HtmlPanel p){
		panel=p;
	}

	private void init(){
		uContext=new SimpleUserAgentContext();
		rContext=new SimpleHtmlRendererContext(panel, uContext);
		docBuilder = new org.lobobrowser.html.parser.DocumentBuilderImpl(uContext,rContext);
		
	}

	public synchronized void setPage(URL url){
		try {
			doc=docBuilder.parse(new InputSourceImpl(url.toString()));
			if(isRequestedPageTest(doc)){
				AppManager.getMainFrame().setContentPanelType(ContentPanelType.TEST);
				NodeList l=doc.getElementsByTagName("test");
				AppManager.getMainFrame().getTestPanel().setTestData(l);
			}else{
				AppManager.getMainFrame().setContentPanelType(ContentPanelType.TEXT);
				panel.setDocument(doc, rContext);
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isRequestedPageTest(Document d){
		return d.getElementsByTagName("test").getLength()>0;
	}

	public ChapterNode.NodeType getNodeTypeForPage(Document d){
		ChapterNode.NodeType n=ChapterNode.NodeType.TEXT;
		if(d.getElementsByTagName("test").getLength()>0){
			String type=d.getElementsByTagName("test").item(0).getAttributes().getNamedItem("type").getNodeValue();
			if("quiz".equals(type))n=ChapterNode.NodeType.QUIZ;
			else if("drag_drop".equals(type))n=ChapterNode.NodeType.DRAGDROP;
			else if("sim".equals(type))n=ChapterNode.NodeType.SIM;
			else if("free_sim".equals(type))n=ChapterNode.NodeType.FREESIM;
		}
		return n;
	}
	
	public Document getDocumentforURI(URI uri) throws SAXException, IOException{
		return docBuilder.parse(uri.toString());
	}
	
	public String getTitleForDoc(Document d){
		return d.getElementsByTagName("title").item(0).getTextContent();
	}
	
	public synchronized String getTitleForURI(URI uri){
		synchronized (docBuilder){
			String result="Not Defined";
			try {
				doc = docBuilder.parse(uri.toString());
				result=doc.getElementsByTagName("title").item(0).getTextContent();
				doc=null;
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return result;
		}
	}

	public String toString(){
		return "";
	}
}
